package com.ruoyi.app.service.impl;

import com.baomidou.mybatisplus.extension.api.R;
import com.ruoyi.app.service.AppEquipmentManagementService;
import com.ruoyi.common.core.domain.JSONObjectPro;
import com.ruoyi.common.enums.YesNoEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.system.domain.EquipmentManagement;
import com.ruoyi.system.domain.vo.equipment.EquipmentInfo;
import com.ruoyi.system.service.IEquipmentManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 描述 APP设备管理Service实现类
 * 作者 ZhangPing
 * 创建时间 2023/10/9 10:39
 */
@Service
public class AppEquipmentManagementServiceImpl implements AppEquipmentManagementService {

    @Autowired
    private IEquipmentManagementService equipmentManagementService;

    /**
     * 校验设备号是否存在
     */
    @Override
    public JSONObjectPro checkExistDeviceId(String deviceId){
        EquipmentManagement equipment = equipmentManagementService.getByDeviceId(deviceId);
        if (equipment == null) throw new ServiceException("设备号不存在!");
        if (equipment.getActivateStatus() == YesNoEnum.YES.getCode()) throw new ServiceException("设备号已被占用!");
        JSONObjectPro resJson = new JSONObjectPro()
                .put("equipmentId", equipment.getId())
                .put("deviceId", equipment.getDeviceId())
                .put("deviceModel", equipment.getDeviceModel())
                .put("simId", equipment.getSimId())
                .put("channelsNum", equipment.getChannelsNum())
                .put("deviceModelStr", DictUtils.getDictLabel("deviceModel", equipment.getDeviceModel()));
        return resJson;
    }

    /**
     * 激活设备
     */
    @Override
    public void activateDevice(EquipmentInfo param){
        EquipmentManagement equipment = equipmentManagementService.getByDeviceId(param.getDeviceId());
        if (equipment == null) throw new ServiceException("设备号不存在!");
        if (equipment.getActivateStatus() == 1) throw new ServiceException("设备号已被占用!");
        equipmentManagementService.updateEquipment(param);
    }
}
